#include "IOSUnit/IOSHelper.h"

#include "AppDelegate.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"


#include "GameScene.h"
#include "LoginScene.h"
#include "GameGlobal.h"
#include "GameDefine.h"

#include "RoomScene.h"

USING_NS_CC;
using namespace CocosDenshion;

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
	//    SimpleAudioEngine::end();
}

bool AppDelegate::applicationDidFinishLaunching()
{
	// initialize director
	CCDirector *pDirector = CCDirector::sharedDirector();
	pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());

	// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
	// pDirector->enableRetinaDisplay(true);

	// sets opengl landscape mode
	// tests set device orientation in RootViewController.mm
	// pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);

	// turn on display FPS
	//pDirector->setDisplayStats(true);

	// set FPS. the default value is 1.0/60 if you don't call this
	pDirector->setAnimationInterval(1.0 / 60);

	//pDirector->setDepthTest(false);
	// create a scene. it's an autorelease object
	//CCScene *pScene = HelloWorld::scene();

	// run
	//pDirector->runWithScene(pScene);

	

	float width = 1024;
	float height = 768;
	float resScale = 1.0;
	bool  bRetain = false;

#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	WSAData d;
	WSAStartup(MAKEWORD(2, 2), &d);

	/*
	CCDirector::sharedDirector()->enableRetinaDisplay(true);
	bRetain = true;
	width    = 480;
	height   = 320;
	resScale = 0.5;
	*/
#endif

	IOSDevice iosDevice;
	iosDevice = CIOSHelper::CheckDeviceType();
	if(IOS_DEVICE_IPOD == iosDevice || IOS_DEVICE_IPHONE == iosDevice)
	{
		width    = 480;
		height   = 320;
		resScale = 0.5;

		CCDirector::sharedDirector()->enableRetinaDisplay(true);
		bRetain = true;
	}

	CGameGlobal::GetInstance()->InitGlobalData(SCREEN_WIDTH, SCREEN_HEIGHT, resScale, width, height, bRetain);

	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Chars/hurt.plist");
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Chars/bows.plist");
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Chars/chars.plist");
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Chars/arrows.plist");
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Scenes/scene2.plist");

	CCDirector::sharedDirector()->setDepthTest(false);
#ifdef GAME_USEAI
	CGameScene::Show();
#else
	CLoginScene::Show();
	//CRoomScene::Show();
#endif
	return true;

	return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
	CLoginScene::Show();
	CCDirector::sharedDirector()->pause();
	SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
	SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
	CCDirector::sharedDirector()->resume();
	SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
	SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
  
